﻿/*
 * Copyright (C) 2010 Jakub Miara
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Net.Araim.FavouriteLocations
{
    /// <summary>
    /// manage favourites class
    /// </summary>
    public partial class ManageFavs : Form
    {
        
        private IBindingList originalFavs;
        /// <summary>
        /// Gets the fav list.
        /// </summary>
        /// <value>The fav list.</value>
        public HashSet<FavouriteLocation> FavList
        {
            get
            {
                HashSet<FavouriteLocation> list = new HashSet<FavouriteLocation>();
                foreach (DataGridViewRow row in this.dataGridView1.Rows)
                {
                    if (!string.IsNullOrWhiteSpace((string)row.Cells[0].Value) && !string.IsNullOrWhiteSpace((string)row.Cells[1].Value))
                    {
                        list.Add(new FavouriteLocation { Name = (string)row.Cells[0].Value, Path = (string)row.Cells[1].Value });
                    }
                }
                return list;
            }
        }



        /// <summary>
        /// Initializes a new instance of the <see cref="ManageFavs"/> class.
        /// </summary>
        /// <param name="favs">The favs.</param>
        public ManageFavs(HashSet<FavouriteLocation> favs)
        {
            InitializeComponent();
            originalFavs = new SortableBindingList<FavouriteLocation>(favs.ToList<FavouriteLocation>());
            this.dataGridView1.DataSource = originalFavs;
        }

        /// <summary>
        /// Handles the RowValidating event of the dataGridView1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.DataGridViewCellCancelEventArgs"/> instance containing the event data.</param>
        private void dataGridView1_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
        {
            if (e.ColumnIndex == 0)
            {
             
                string value = (string)dataGridView1.Rows[e.RowIndex].Cells[0].Value;
                
                    if (originalFavs.Cast<FavouriteLocation>().Distinct<FavouriteLocation>(new FavouriteLocationComparer()).Count() != originalFavs.Count)
                    {
                        MessageBox.Show(value + " already exists among your favourites, choose another name.");
                        e.Cancel = true;
                        return;
                    }
                
            }
        }

        /// <summary>
        /// Handles the Click event of the button2 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void button2_Click(object sender, EventArgs e)
        {
            
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
           
        }

        /// <summary>
        /// Handles the Click event of the toolStripMenuItem1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (DataGridViewRow r in this.dataGridView1.SelectedRows)
                {
                    if (!r.IsNewRow)
                    {
                        this.dataGridView1.Rows.Remove(r);
                    }
                }
            }
            catch (Exception) { 
                //ignore
            }
        }

        /// <summary>
        /// Handles the SortCompare event of the dataGridView1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.DataGridViewSortCompareEventArgs"/> instance containing the event data.</param>
        private void dataGridView1_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
        {

            e.SortResult = e.CellValue1.ToString().CompareTo(e.CellValue2.ToString());
        }
    }
}
